package pe.com.bbva.targps.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import pe.com.bbva.targps.dao.ConsultaGPSDAO;
import pe.com.bbva.targps.domain.ConsultaGPS;
import pe.com.bbva.util.Constantes;
import pe.com.stefanini.core.dao.GenericDAOImpl;
import pe.com.stefanini.core.exceptions.BOException;
import pe.com.stefanini.core.exceptions.DAOException;


@Service("consultaGPSDAO")
public class ConsultaGPSDAOImpl extends GenericDAOImpl<ConsultaGPS> implements ConsultaGPSDAO{
	Logger logger = Logger.getLogger(this.getClass());
	
	@Autowired
	public ConsultaGPSDAOImpl(SessionFactory sessionFactory) {
		super(sessionFactory);
	}
	
	
	@SuppressWarnings("unchecked")
	public List<ConsultaGPS> findProcesosCargaRango(ConsultaGPS consultaGPS, String fechaInicio, String fechaFin)throws BOException, DAOException {
		String where = "";
		if(fechaInicio!=null && !fechaInicio.equals("")){
			if(fechaFin!=null && !fechaFin.equals("") && !fechaFin.equals(fechaInicio)){
				where = where + (where==""?"":" AND ") + "fechaCreacion BETWEEN to_date('" + fechaInicio + "','dd/mm/yyyy') and to_date('" + fechaFin + "','dd/mm/yyyy')";
			}else{
				where = where + (where==""?"":" AND ") + "to_char(fechaCreacion,'dd/mm/yyyy') = '" + fechaInicio + "' ";				
			}
		}
		if(consultaGPS.getFecha()!= null && !consultaGPS.getFecha().equals("")){
			where = where + (where==""?"":" AND ") + "fecha = " + consultaGPS.getFecha();
		}
		if(consultaGPS.getHora()!= null && !consultaGPS.getHora().equals("")){
			where = where + (where==""?"":" AND ") + "hora = " + consultaGPS.getHora();
		}
		if(consultaGPS.getEstado()!= null && !consultaGPS.getEstado().equals("")){
			where = where + (where==""?"":" AND ") + "estado = '" + consultaGPS.getEstado()+ "'";
		}
		if(consultaGPS.getUsuario()!= null && !consultaGPS.getUsuario().equals("")){
			where = where + (where==""?"":" AND ") + "usuario = '" + consultaGPS.getUsuario()+ "'";
		}
		if(consultaGPS.getTipoGeneracion()!= null && !consultaGPS.getTipoGeneracion().equals("")){
			where = where + (where==""?"":" AND ") + "tipoGeneracion = '" + consultaGPS.getTipoGeneracion()+ "'";
		}
		if(consultaGPS.getNombreArchivo()!= null && !consultaGPS.getNombreArchivo().equals("")){
			where = where + (where==""?"":" AND ") + "nombreArchivo = '" + consultaGPS.getNombreArchivo()+ "'";
		}
		if(!where.equals("")){
			where = Constantes.WHERE_CLAUSE + where;
		}
		String orders = " order by fechaCreacion desc";
		List<ConsultaGPS> listaProcesos = super.executeQuery(ConsultaGPS.class,where,orders);
		return listaProcesos;
	}

}